package com.yanqu.road.server.cmd.activity.relation;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.CrossRelationActivityMgr;
import com.yanqu.road.server.manager.activity.relation.CrossRelationRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.C_CROSS_RELATION_ACTIVITY_GET_RANK_REWARD, desc = "领取小当家类型活动排行奖励")
public class CrossGetRankRewardCmd extends CrossCommand {
    private int activityId;
    private int conditionId;
    private long userId;
    private String unionUid;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossActivityGetRewardReqMsg reqMsg = CrossActivityProto.CrossActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionId = reqMsg.getConditionId();
        unionUid = reqMsg.getUnionUid();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int myRank = -1;
        int pos = 0;
        ActivityConditionInfo activityConditionInfo = CrossRelationActivityMgr.getActivityConditionInfo(activityId, conditionId);
        if(null != activityConditionInfo){
            if(activityConditionInfo.getType() == CrossRelationActivityMgr.getEventConditionType(activityId,CrossRelationActivityMgr.EVENT_RANK_USER_TYPE)) {
                myRank = CrossRelationRankMgr.getCrossActivityRank(activityId, serverId, userId);
            }else if(activityConditionInfo.getType() == CrossRelationActivityMgr.getEventConditionType(activityId,CrossRelationActivityMgr.EVENT_RANK_SERVER_TYPE)) {
                myRank = CrossRelationRankMgr.getCrossActivityServerRank(activityId, serverId, userId);
            }else {
                UnionActivityConditionData unionActivityConditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, activityConditionInfo.getType(), userId);
                if (unionActivityConditionData != null) {
                    unionUid = unionActivityConditionData.getUnionUid();
                    Map<Long, Integer> memberPositionMap = unionActivityConditionData.getMemberPositionMap();
                    if (memberPositionMap != null) {
                        Integer value = memberPositionMap.get(userId);
                        if (value != null) {
                            pos = value;
                        }
                    }
                }
                myRank = CrossRelationRankMgr.getCrossActivityUnionRank(activityId, serverId, unionUid);
            }
        }
        CrossActivityProto.CrossActivityGetRewardRespMsg.Builder respMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setConditionId(conditionId);
        respMsg.setRank(myRank);
        respMsg.setPosition(pos);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_COOK_BOY_GET_RANK_REWARD_NOTIFY, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {

    }
}
